load (i.e. source) our rescale() function from last day
source("http://tinyurl.com/rescale-R")
test this function
rescale(1:5)
## [1] 0.00 0.25 0.50 0.75 1.00
#' rescale(c(1:5, "string"))
We want to make this function more robust to these “string”" types of errors two functions to use, warning() and stop()
#rescale2(c(1:5, "string"))
is.numeric(1:5)
## [1] TRUE
is.numeric("string")
## [1] FALSE
is.numeric(c(1:5, "string"))
## [1] FALSE
!is.numeric(1:5)
## [1] FALSE
x <- c( 1, 2, NA, 3, NA)
y<-c(NA,3,NA,3, 4)
is.na(x)
## [1] FALSE FALSE TRUE FALSE TRUE
is.na(y)
## [1] TRUE FALSE TRUE FALSE FALSE
sum(is.na (x) & is.na (y)) #this is a snippet
## [1] 1
# which(is.na(x) & is.na(y)) indicates where in the vector is the true element
Now take our working snippet and make first function
both_na <- function (x,y) {
# Check for NA elements in both input vectors
sum(is.na (x) & is.na (y))
}
x <- c( NA, NA, NA)
y1 <- c( 1, NA, NA)
y2 <- c( 1, NA, NA, NA)
y3 <- c( 1, NA, NA, NA, NA)
# What will this return?
both_na(x,y2)
## Warning in is.na(x) & is.na(y): longer object length is not a multiple of
## shorter object length
## [1] 3
When comparing x with y2, the last NA with y2 has no paring, so the first element of x is added on to the end of x to pair with y2.
both_na <- function (x, y) {
if(length(x) != length(y)) {
stop("Input x and y should be vectors of the same length")
# be easily missed especially in scripts.
}
na.in.both <- ( is.na(x) & is.na(y) )
na.number <- sum(na.in.both)
na.which <- which(na.in.both)
message("Found ", na.number, " NA's at position(s):",
paste(na.which, collapse=", ") )
return( list(number=na.number, which=na.which) )
}
library("bio3d")
data <- system.file("examples/ediaspora.gexf.xml", package = "bio3d")
#sigma(data)
#install.packages("ggplot2")
#install.packages("plotly")
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
p <- ggplot(data = diamonds, aes(x = cut, fill = clarity)) +
geom_bar(position = "dodge")
ggplotly(p)